﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DARE
{
    public class CCurveModel : CBasicModel
    {

        #region fields

        private int m_sub;
        private CCurve m_curve;

        #endregion

        #region properties

        #endregion

        #region ctor

        public CCurveModel()
        {
            Initialize(null, 0);
        }

        public CCurveModel(Vector3 start, Vector3 ctrlA, Vector3 ctrlB, Vector3 end)
            : this(start, ctrlA, ctrlB, end, 15)
        {

        }

        public CCurveModel(Vector3 start, Vector3 ctrlA, Vector3 ctrlB, Vector3 end, int sub) : 
            base(1, 1, 1)
        {
            Initialize(new CCurve(start, ctrlA, ctrlB, end), sub);
        }

        public CCurveModel(CCurve curve)
            :this(curve, 15)
        {

        }

        public CCurveModel(CCurve curve, int sub) :
            base(1, 1, 1)
        {
            Initialize(curve, sub);
        }

        private void Initialize(CCurve curve, int sub)
        {
            if (curve == null)
                return;
            m_sub = sub;
            m_curve = curve;
            SetUpVertices();
            SetUpIndices();
            SetUpEffect();
            CreateVertexBuffer();
            CreateIndexBuffer();
        }

        #endregion

        #region methods

        private void SetUpVertices()
        {
            int index = 0;
            float step = (float)1 / (float)(m_sub);
            float t = 0;

            m_primitiveType = Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList;

            // allocate the vertices
#if WINDOWS_PHONE
            m_vertices = new VertexPositionNormalTexture[m_sub + 1];
            for (int i = 0; i < m_sub + 1; ++i)
                m_vertices[index++] = new VertexPositionNormalTexture();
#else
            m_vertices = new VertexPositionNormalTextureTangentBinormal[m_sub + 1];
            for (int i = 0; i < m_sub + 1; ++i)
                m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal();
#endif
            index = 0;

            // we cannot garanty that t will be exaclty 1 at the end because of float approximation so we skip the last compute 
            // and do it after with 1
            for (t = 0; t < 1; t += step)
                m_vertices[index++].Position = m_curve.GetPos(t);
            if (t == 1)
                m_vertices[index].Position = m_curve.GetPos(1);
            else if (t < 1)
                m_vertices[index].Position = m_curve.GetPos(1);
            else if (t > 1)
                m_vertices[index - 1].Position = m_curve.GetPos(1);
        }

        private void SetUpIndices()
        {
            int index = 0;

            m_indices = new short[m_sub * 2];
            for (int i = 0; i < m_sub; ++i)
            {
                m_indices[index++] = (short)i;
                m_indices[index++] = (short)(i + 1);
            }
        }

        private void SetUpEffect()
        {
            // disable the "real" light and set an emissive
            m_effect.UseSceneParameters = false;
            m_effect.LighteningEnabled = false;
            m_effect.EmissiveLightColor = Color.White;
            m_effect.EmissiveLightIntensity = 1.0f;

            // set the color
            Color = Color.OrangeRed;
            //Color = Color;
        }

        #endregion

    }
}
